package com.itsxb.controller.app;

import cn.dev33.satoken.annotation.SaIgnore;
import com.itsxb.constants.GlobalExceptionCode;
import com.itsxb.exception.GlobalException;
import com.itsxb.result.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import template.CosTemplate;

import javax.validation.Valid;
import java.io.IOException;

@RestController
@Tag(name = "文件上传", description = "文件上传")
//@SpringBootApplication注解 为什么不报错
@RequestMapping("/app/file")
public class FileController {
    @Autowired
    private CosTemplate cosTemplate;
    //而这个返回的就是FileUrl地址 给前端
    @PostMapping("/cos-upload")
    @Operation(summary = "文件上传", description = "文件上传接口")
    @Parameter(name = "file", description = "文件上传", required = true)
    @SaIgnore
    public R wechatRegister(@RequestParam("file") MultipartFile file) {
        String originalName = file.getOriginalFilename();
        try {
            String fileUrl = cosTemplate.testUpload(file.getInputStream(), originalName);
            return R.success(fileUrl);
        } catch (IOException e) {
            throw new GlobalException(GlobalExceptionCode.SERVICE_ERROR);
        }
    }

}
